package com.blustreaksw.cyclecoach.history;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.blustreaksw.cyclecoach.R;

public class WorkoutHistoryFragment extends ListFragment {
	private ArrayList<WorkoutLogEntry> mWorkoutHistory;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(R.string.subactivity_workoutHistory_title);
        mWorkoutHistory = WorkoutHistoryContainer.get(getActivity()).getAllLogEntries();
        WorkoutLogEntryAdapter adapter = new WorkoutLogEntryAdapter(mWorkoutHistory);
        setListAdapter(adapter);
        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        setHasOptionsMenu(true);
    }
	
	public void onListItemClick(ListView l, View v, int position, long id) {
        // get the log entry from the adapter
        WorkoutLogEntry w = ((WorkoutLogEntryAdapter)getListAdapter()).getItem(position);
        // start an instance of WorkoutLogEntryActionSwipeActivity
        Intent i = new Intent(getActivity(), WorkoutLogEntryActionSwipeActivity.class);
        i.putExtra(WorkoutLogEntryFragment.EXTRA_WORKOUT_ENTRY_ID, w.getId());
        startActivityForResult(i, 0);
    }
	
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        ((WorkoutLogEntryAdapter)getListAdapter()).notifyDataSetChanged();
    }
    
    private class WorkoutLogEntryAdapter extends ArrayAdapter<WorkoutLogEntry> {
        public WorkoutLogEntryAdapter(ArrayList<WorkoutLogEntry> history) {
            super(getActivity(), android.R.layout.simple_list_item_1, history);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // if we weren't given a view, inflate one
            if (null == convertView) {
                convertView = getActivity().getLayoutInflater()
                    .inflate(R.layout.list_item_workout_log_entry, null);
            }

            // configure the view for this log entry
            WorkoutLogEntry w = getItem(position);

            TextView dateTextView =
                (TextView)convertView.findViewById(R.id.workoutLogEntryListItem_dateTextView);
            dateTextView.setText(w.getDate().toString());
            
            TextView timeElapsed =
                (TextView)convertView.findViewById(R.id.workoutLogEntryListItem_timeElapsedView);
            timeElapsed.setText(w.getTimeElapsed().toString());
            
            TextView distance =
            		(TextView)convertView.findViewById(R.id.workoutLogEntryListItem_distView);
            distance.setText(Double.toString(w.getDistanceTraveled()));
            
            TextView speed =
            		(TextView)convertView.findViewById(R.id.workoutLogEntryListItem_speedView);
            speed.setText(Double.toString(w.getSpeedAvg()));

            return convertView;
        }
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    		case android.R.id.home:
    			if(NavUtils.getParentActivityName(getActivity()) != null) {
    				NavUtils.navigateUpFromSameTask(getActivity());
    			}
    			return true;
    		
    		default:
    			return super.onOptionsItemSelected(item); 
    	}
    }

}
